package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private DtsRoleMapper roleMapper;

    @Override
    public Set<String> queryByIds(Integer[] ids) {

        //判断如果参数为null, 直接返回空的角色名称对象
        if (ids == null || ids.length == 0) {
            return new HashSet<String>();
        }

        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        //查询条件相当于where id in(1,2,3,4)
        queryWrapper.in("id", ids);
        //查询没有被删除的
        queryWrapper.eq("deleted", false);
        //查询启用的角色
        queryWrapper.eq("enabled", true);

        List<DtsRole> list = roleMapper.selectList(queryWrapper);

        //将list装换成shiro框架需要的set集合
        Set<String> roleSet = new HashSet<>();
        if (list != null) {
            for (DtsRole role : list) {
                roleSet.add(role.getName());
            }
        }
        return roleSet;
    }

}
